<?php

class zameleon {

	var $sql  	= '';
	var $sess 	= '';
	var $skin 	= '';
	var $cache 	= '';
	var $url  	= 'http://localhost:8888/new.zam/';
	var $db  	= array(
		'localhost' => 'localhost:8889',
		'username'  => 'root',
		'password'  => 'root',
		'database'  => 'zameleon'
	);
	
	#var $lang = array();
	var $data = array();

	#=============================
	# Init combiner class
	#=============================
	function zameleon()
	{
		#==================================
		# MySQL, template, session modules
		#==================================
		$modules = array(
			'sql',
			'skin',
			'tags',
			'sess',
			'cache',
			'email'
		);
		
		while ( list(,$value) = each($modules) )
		{
			$this->load_module($value);
		}
		
		# SQL Login
		$this->sql->connect(
			$this->db['localhost'],
			$this->db['username'],
			$this->db['password'],
			$this->db['database']
		);
		
		# Recreate and clean variables
		$this->parse_vars();
		
		# Start/update session & cleanup old sessions
		$this->sess->init_session();
		$this->sess->cleanup_sessions();
		
		# Skin data
		$this->skin->config['tpl_path'] = constant('SITE_PATH') . "html/";
		$this->skin->set_var('url', $this->url);
		
		#$this->load_lang('general');
		#$this->skin->set_var('lang', $this->lang);
		$this->skin->set_var('is_logged_in', $this->sess->is_logged_in());
	}
	
	#==================================
	# Load modules
	#==================================
	function load_module($mod_name)
	{
		require_once constant('SITE_PATH') . 'includes/' . $mod_name . '.php';
		$this->$mod_name = new $mod_name();
		$this->$mod_name->zam = &$this;
	}
	
	#==================================
	# Load language module
	# (Currently not in use)
	#==================================
	/*function load_lang($lang_mod)
	{
		$langs = array(
			'en'	=>	'en',
			'fr'	=>	'fr'
		);
		
		if ( !array_key_exists($this->sess->user['slang'], $langs) )
		{
			$dl = 'en';
		}
		else
		{
			$dl = $langs[ $this->sess->user['slang'] ];
		}
		
		$dlpath = constant('SITE_PATH') . 'lang/' . $dl . '/' . $lang_mod . '.php';
		if ( file_exists( $dlpath ) )
		{
			require_once constant('SITE_PATH') . 'lang/' . $dl . '/' . $lang_mod . '.php';
			$lang = new $lang_mod( $this->url );
			$this->lang = @array_merge($this->lang, $lang->data);
		}
		else
		{
			$this->kill("missing lang module ($lang_mod)");
		}
	}*/
	
	#==================================
	# Clean and create vars
	#==================================
	function parse_vars()
  	{
    	$this->data['post'] = $_POST;
		$this->data['get'] = $_GET;
		$this->data['cookie'] = $_COOKIE;
		$URI = parse_url( 'http://localhost' . str_replace( 'new.zam/', '', $_SERVER['REQUEST_URI'] ) );
		$this->data['params'] = explode('/', $URI['path']);
	
		while ( list($key, $value) = each($this->data) )
		{
			while ( list($k, $v) = each($this->data[$key]) )
			{
				$this->data[$key][$k] = trim( stripslashes( htmlspecialchars( $this->data[$key][$k], ENT_QUOTES ) ) );
			}
		} 
  	}
	
	#==================================
	# Kill kernel and print error
	#==================================
	function kill($error='')
	{
		$this->cache->build_cache('error_log', $error);
		exit("<b>An error has occured:</b> $error\n");
	}
	
}

?>